#include "ClientSocket.h"


ClientSocket::ClientSocket(SOCKET sockfd): Socket(sockfd)
{
}

ClientSocket::ClientSocket()
{
}


ClientSocket::~ClientSocket()
{
}

void ClientSocket::Connect(const std::string& server, unsigned short port)
{
	struct sockaddr_in serveraddr;
	struct hostent *he;

	unsigned long addrlong = inet_addr(server.c_str());

	if (addrlong != INADDR_NONE)
	{
	    he = gethostbyaddr((char*)&addrlong, sizeof(addrlong), AF_INET);
	}
	else
	{
		he = gethostbyname(server.c_str());
	}

    if (he == NULL) 
	{
		throw SocketException();
    }

	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(port);
	serveraddr.sin_addr = *((struct in_addr *)he->h_addr);
	memset(serveraddr.sin_zero, 0, sizeof(serveraddr.sin_zero));

	if (connect(GetSocket(), (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1) 
	{
		throw SocketException();
	}
}

